package cn.stylefeng.guns.sys.modular.personInfo.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.personInfo.entity.PersonInfo;
import cn.stylefeng.guns.sys.modular.personInfo.mapper.PersonInfoMapper;
import cn.stylefeng.guns.sys.modular.personInfo.model.params.PersonInfoParam;
import cn.stylefeng.guns.sys.modular.personInfo.model.result.PersonInfoResult;
import cn.stylefeng.guns.sys.modular.personInfo.service.PersonInfoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.net.ContentHandler;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 人员表 服务实现类
 * </p>
 *
 * @author WangZhen
 * @since 2021-01-20
 */
@Service
public class PersonInfoServiceImpl extends ServiceImpl<PersonInfoMapper, PersonInfo> implements PersonInfoService {

    @Override
    public void add(PersonInfoParam param) {
        LoginUser user = LoginContextHolder.getContext().getUser();
        param.setIsDel(0);
        param.setCreatePerson(user.getName());
        param.setCreateDate(new Date());
        this.baseMapper.insertPerson(param);
    }

    @Override
    public void delete(PersonInfoParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(PersonInfoParam param) {
        PersonInfo oldEntity = getOldEntity(param);
        PersonInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public PersonInfoResult findBySpec(PersonInfoParam param) {
        return null;
    }

    @Override
    public List<PersonInfoResult> findListBySpec(PersonInfoParam param) {
        param.setIsDel(0);
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(PersonInfoParam param) {
        Page pageContext = getPageContext();
        param.setIsDel(0);
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public Integer selectDeleteInfo(Integer param) {
        return this.baseMapper.selectDelete(param);
    }

    @Override
    public void updateDeleteInfo(Integer param) {
        this.baseMapper.updateDelete(param);
    }

    @Override
    public void updatePersonInfo(PersonInfoParam param) {
        this.baseMapper.updatePerson(param);
    }

    private Serializable getKey(PersonInfoParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private PersonInfo getOldEntity(PersonInfoParam param) {
        return this.getById(getKey(param));
    }

    private PersonInfo getEntity(PersonInfoParam param) {
        PersonInfo entity = new PersonInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
